<?php

namespace App\Exports;

use App\Models\Student\Point;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;

class PointStudentExport extends DefaultValueBinder implements FromArray,WithCustomValueBinder
{

    private $conditions;

    public function __construct(array $conditions) {
        $this->conditions = $conditions;
    }

    public function array():array  {
        $data = [
            [
                'ID',
                '姓名',
                '电话',
                '邮箱',
                '志愿学校',
                '志愿专业',
                '奖学金',
                '预计总学费',
                '调剂学校',
                '调剂专业',
                '奖学金',
                'GPA',
                '复试总成绩',
                '预计总学费',
                '面试成绩',
                '来源',
            ]
        ];

        $records = (new Point())->getRecords($this->conditions)->get();

        foreach ($records as &$record) {
            $temp = $record->toArray();
            unset($temp['guard_user_id']);
            unset($temp['created_at']);
            unset($temp['updated_at']);
            $data[] = $temp;
        }
        return $data;
    }

    public function bindValue(Cell $cell, $value) {
        if (strlen($value) > 10) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
            return true;
        }
        return parent::bindValue($cell, $value);
    }
}
